package org.quickjsf.core.base.viewmodel.support;

import java.io.Serializable;

import org.springframework.beans.BeanUtils;

public abstract class EditableViewModelSupport<T> extends AbsDTPanelViewModelSupport<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4239105720581456396L;
	
	private T entity;
	
	public EditableViewModelSupport() {
		try {
			entity = getDTPanelViewModel().getEntityCls().newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void resetEntity() throws Exception {
		entity = getDTPanelViewModel().getEntityCls().newInstance();
	}
	
	public void setSelectedValToEntity() throws Exception {
		T target = getDTPanelViewModel().getEntityCls().newInstance();
		BeanUtils.copyProperties(getDTPanelViewModel().getSelectdEntity(), target);
		setEntity(target);
	}

	public T getEntity() {
		return entity;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}
}
